package com.example.zhilongzhang.constructionpayapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zhilongzhang.constructionpayapp.R;
import com.example.zhilongzhang.constructionpayapp.base.ActivityCache;
import com.example.zhilongzhang.constructionpayapp.base.BaseActivity;
import com.example.zhilongzhang.constructionpayapp.bean.LoginBean;
import com.example.zhilongzhang.constructionpayapp.dialog.WaitDialog;
import com.example.zhilongzhang.constructionpayapp.http.Bean;
import com.example.zhilongzhang.constructionpayapp.http.HttpMethod;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

public class ResetPasswordActivity extends BaseActivity implements View.OnClickListener {

    ImageView ivBack,ivCancelOldPassword,ivCancelNewPassword,ivCancelMakenewPassword;
    EditText etOldPassword;
    EditText etNewPassword;
    EditText etMakenewPassword;
    TextView tvFinish;
    WaitDialog mDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpassword);
        ivBack = findViewById(R.id.left);
        etOldPassword = findViewById(R.id.et_old_password);
        ivCancelOldPassword = findViewById(R.id.cancel_old_password);
        etNewPassword = findViewById(R.id.et_new_password);
        ivCancelNewPassword = findViewById(R.id.cancel_new_password);
        etMakenewPassword = findViewById(R.id.et_makenew_password);
        ivCancelMakenewPassword = findViewById(R.id.cancel_makenew_password);
        tvFinish = findViewById(R.id.tv_finish);

        ivBack.setOnClickListener(this);
        etOldPassword.setOnClickListener(this);
        ivCancelOldPassword.setOnClickListener(this);
        etNewPassword.setOnClickListener(this);
        ivCancelNewPassword.setOnClickListener(this);
        etMakenewPassword.setOnClickListener(this);
        ivCancelMakenewPassword.setOnClickListener(this);
        tvFinish.setOnClickListener(this);

        // 显隐(旧密码)
        etOldPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    ivCancelOldPassword.setVisibility(View.VISIBLE);
                } else {
                    ivCancelOldPassword.setVisibility(View.INVISIBLE);
                }
            }
        });

        // 显隐(新密码)
        etNewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    ivCancelNewPassword.setVisibility(View.VISIBLE);
                } else {
                    ivCancelNewPassword.setVisibility(View.INVISIBLE);
                }
            }
        });

        // 显隐(再次确认密码)
        etMakenewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    ivCancelMakenewPassword.setVisibility(View.VISIBLE);
                } else {
                    ivCancelMakenewPassword.setVisibility(View.INVISIBLE);
                }
            }
        });

        // 旧密码全部删除
        ivCancelOldPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etOldPassword.setText("");
            }
        });

        // 新密码全部删除
        ivCancelNewPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etNewPassword.setText("");
            }
        });

        // 再次确认密码全部删除
        ivCancelMakenewPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etMakenewPassword.setText("");
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left:
                finish();
                break;
            case R.id.tv_finish:
                resetPassword();
                break;
        }
    }

    private void resetPassword() {

        String psw=etOldPassword.getText().toString().trim();
        String newPsw=etNewPassword.getText().toString().trim();
        String again=etMakenewPassword.getText().toString().trim();

        if (!TextUtils.isEmpty(etNewPassword.getText().toString())
                && TextUtils.equals(etNewPassword.getText().toString(), etMakenewPassword.getText().toString())) {

            mDialog = new WaitDialog(ResetPasswordActivity.this);
            mDialog.show();

            HttpMethod.resetPassword(" ",etOldPassword.getText().toString(),etNewPassword.getText().toString(), new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {
                    mDialog.dismiss();
                    Toast.makeText(ResetPasswordActivity.this, "网络连接异常，请检查您的网络", Toast.LENGTH_LONG).show();
                }
                @Override
                public void onResponse(String response) {
                    mDialog.dismiss();
                    Bean bean = new Gson().fromJson(response, Bean.class);
                    if (bean.code == 0) {
                        Toast.makeText(ResetPasswordActivity.this, "修改密码成功，请您重新登录", Toast.LENGTH_LONG).show();
                        ActivityCache.getScreenManager().popAllActivityExceptOne(LoginActivity.class);
                        startActivity(new Intent(ResetPasswordActivity.this, LoginActivity.class));
                    }else{
                        Toast.makeText(ResetPasswordActivity.this, "旧密码不正确，请重新输入", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }else{
            Toast.makeText(ResetPasswordActivity.this, "两次填写的密码不一致", Toast.LENGTH_LONG).show();
        }
  }
}
